package com.chenexample.myshop16173.fragment;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chenexample.myshop16173.CollectionActivity;
import com.chenexample.myshop16173.ExchangeActivity;
import com.chenexample.myshop16173.GlideImageLoader;
import com.chenexample.myshop16173.GoodDetailActivity;
import com.chenexample.myshop16173.R;
import com.chenexample.myshop16173.SearchActivity;
import com.chenexample.myshop16173.myview.MyGridView;
import com.chenexample.myshop16173.util.GetImage;
import com.chenexample.myshop16173.util.MyApplication;
import com.chenexample.myshop16173.util.SPUtils;
import com.chenexample.myshop16173.util.util;

import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class HomeFragment extends Fragment {

    private MyApplication application;
    private MyGridView gridView;
    private MyGridView gridViewToday;
    private MyGridView gridViewSale;
    private MyGridView gridViewNew;
    private ImageView clock;
    private MyAdapterForGoods myAdapterForToday;
    private MyAdapterForGoods myAdapterForSale;
    private MyAdapterForGoods myAdapterForNew;
    private List<Map<String, Object>> menuList;
    private List<Map<String, Object>> todayList;
    private List<Map<String, Object>> saleList;
    private List<Map<String, Object>> newList;
    private final int KIND_TODAY = 1;
    private final int KIND_SALES = 2;
    private final int KIND_NEW = 3;
    private final int KIND_KIND = 4;
    private LoginAC ac;
    private boolean first;
    private boolean canRefresh;
    private TextView search;
    private LinearLayout integral, recommend, premium, motion;

    public HomeFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_home, container, false);
        canRefresh = true;
        first = true;
        application = (MyApplication) getActivity().getApplication();
        gridViewToday = (MyGridView) v.findViewById(R.id.GridView_fhome_today);
        gridViewSale = (MyGridView) v.findViewById(R.id.GridView_fhome_sale);
        gridViewNew = (MyGridView) v.findViewById(R.id.GridView_fhome_new);
        search = v.findViewById(R.id.search);
        integral = v.findViewById(R.id.integral);
        recommend = v.findViewById(R.id.recommend);
        premium = v.findViewById(R.id.premium);
        motion = v.findViewById(R.id.motion);
        motion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //运动健康
                Toast.makeText(getActivity(), "暂未开放，尽请期待！", Toast.LENGTH_LONG).show();
//                startActivity(new Intent(getActivity(), VolunteerActivity.class));
            }
        });
        premium.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //超值
                Toast.makeText(getActivity(), "暂未开放，尽请期待！", Toast.LENGTH_LONG).show();
            }
        });
        recommend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //推荐
                Toast.makeText(getActivity(), "暂未开放，尽请期待！", Toast.LENGTH_LONG).show();
            }
        });
        integral.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //积分
                startActivity(new Intent(getActivity(), ExchangeActivity.class));
            }
        });
        clock = v.findViewById(R.id.clock);

        clock.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (application.isB_login()) {
                    Intent intent = new Intent(getActivity(), CollectionActivity.class);
                    startActivity(intent);

                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");// HH:mm:ss
                    // 获取当前时间
                    Date date = new Date(System.currentTimeMillis());
                    if (simpleDateFormat.format(date).equals(SPUtils.get(getActivity(), "shijian", ""))) {
                        Toast.makeText(getActivity(), "今日已经打过卡了", Toast.LENGTH_LONG).show();
                    } else {
                        //打卡
                        new QMUIDialog.MessageDialogBuilder(getActivity())
                                .setTitle("签到")
                                .setMessage("确定签到")
                                .addAction("取消", new QMUIDialogAction.ActionListener() {
                                    @Override
                                    public void onClick(QMUIDialog dialog, int index) {
                                        dialog.dismiss();

                                    }
                                })
                                .addAction("确定", new QMUIDialogAction.ActionListener() {
                                    @Override
                                    public void onClick(QMUIDialog dialog, int index) {
                                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");// HH:mm:ss
                                        // 获取当前时间
                                        Date date = new Date(System.currentTimeMillis());
                                        Toast.makeText(getActivity(), "今日打卡成功！\n+10积分", Toast.LENGTH_LONG).show();
                                        int a = (int) SPUtils.get(getActivity(), "jifen", 0);
                                        SPUtils.put(getActivity(), "jifen", a + 10);
                                        SPUtils.put(getActivity(), "shijian", simpleDateFormat.format(date));
                                        dialog.dismiss();
                                    }
                                })
                                .show();
                    }
                } else {
                    Toast.makeText(getContext(), "请先登录！", Toast.LENGTH_LONG).show();
                }
            }
        });
        List<Integer> images = new ArrayList<>();
        images.add(R.drawable.adv_1);
        images.add(R.drawable.adv_2);
        images.add(R.drawable.adv_3);
        List<Integer> images1 = new ArrayList<>();
        images1.add(R.drawable.shopping_2);
        images1.add(R.drawable.shopping_1);
        Banner banner = (Banner) v.findViewById(R.id.homeBanner);
        //设置banner样式
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        banner.setImages(images);
        //设置banner动画效果
        banner.setBannerAnimation(Transformer.Default);
        //设置自动轮播，默认为true
        banner.isAutoPlay(true);
        //设置轮播时间
        banner.setDelayTime(1600);
        //设置指示器位置（当banner模式中有指示器时）
        banner.setIndicatorGravity(BannerConfig.CENTER);
        //banner设置方法全部调用完毕时最后调用
        banner.start();

        ImageView banner1 = (ImageView) v.findViewById(R.id.homeBanner1);
        banner1.setImageResource(R.drawable.shopping_1);

        todayList = new ArrayList<>();
        saleList = new ArrayList<>();
        newList = new ArrayList<>();
        myAdapterForToday = new MyAdapterForGoods(this.getContext(), todayList);
        myAdapterForSale = new MyAdapterForGoods(this.getContext(), saleList);
        myAdapterForNew = new MyAdapterForGoods(this.getContext(), newList);
        gridViewToday.setAdapter(myAdapterForToday);
        gridViewSale.setAdapter(myAdapterForSale);
        gridViewNew.setAdapter(myAdapterForNew);
        ac = new LoginAC();
        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), SearchActivity.class));
            }
        });

    }

    @Override
    public void onResume() {
        super.onResume();
        System.out.println("---------------------------开始了----------------------------------------");
        if (first && canRefresh) {
            canRefresh = false;
            try {
                ac.execute(123);
                first = false;
            } catch (Exception e) {
                System.out.println("这行了二次homeAC");
            }
        }
    }


    class MyAdapterForGoods extends BaseAdapter {

        private List<Map<String, Object>> list;
        private Context context;

        public MyAdapterForGoods(Context context, List<Map<String, Object>> list) {
            super();
            this.list = list;
            this.context = context;
        }

        public void setList(List<Map<String, Object>> list) {
            this.list = list;
        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = LayoutInflater.from(context).inflate(R.layout.item_f_goods, null);
            ImageView imageView = (ImageView) v.findViewById(R.id.imageView_f_goods);
            TextView textViewName = (TextView) v.findViewById(R.id.textView_f_goods_name);
            TextView textViewPre = (TextView) v.findViewById(R.id.textView_f_goods_pre_price);
            TextView textViewPrice = (TextView) v.findViewById(R.id.textView_f_goods_price);
            TextView textViewOver = (TextView) v.findViewById(R.id.textView_f_goods_over);
            if (!list.get(position).containsKey("imgBit") || list.get(position).get("imgBit") == null) {
                imageView.setImageResource(R.drawable.wait);
            } else {
                imageView.setImageBitmap((Bitmap) list.get(position).get("imgBit"));
            }

            textViewName.setText((String) list.get(position).get("name"));
            v.setContentDescription((String) list.get(position).get("id"));
            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 到商品详情
                    Intent intent = new Intent(getContext(), GoodDetailActivity.class);
                    intent.putExtra("goodId", v.getContentDescription());
                    startActivity(intent);
                }
            });
            if (list.get(position).get("pre") == null || ((String) list.get(position).get("pre")).length() == 0) {
                textViewPre.setVisibility(View.INVISIBLE);
            } else {
                textViewPre.setText("商品原价：" + (String) list.get(position).get("pre") + " 元");
            }
            if (list.get(position).get("over") == null || ((String) list.get(position).get("over")).length() == 0) {
                textViewPre.setVisibility(View.GONE);
            } else {
                textViewOver.setText("已售出" + (String) list.get(position).get("over") + "件");
            }
            textViewPrice.setText("￥" + (String) list.get(position).get("price") + " 元");

            return v;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }
    }

    class LoginAC extends AsyncTask<Integer, Object, Object> {

        private ProgressDialog pd;
        private String imgUrlHead;

        @Override
        protected Object doInBackground(Integer... params) {
            int res = 0;
            imgUrlHead = application.getWebURL() + "/images/goods";
            int[] kind = new int[]{KIND_TODAY, KIND_SALES, KIND_NEW, KIND_KIND};
            String uri = null;
            for (int index = 0; index < kind.length; index++) {
                switch (kind[index]) {
                    case KIND_TODAY:
                        uri = "/indexForMobile.action?kind=today";
                        break;
                    case KIND_SALES:
                        uri = "/goods/sale_goodsForMobile.action";
                        break;
                    case KIND_NEW:
                        uri = "/goods/new_goodsForMobile.action";
                        break;
                    case KIND_KIND:
                        uri = "/indexForMobile.action?kind=kinds";
                        break;
                }
                try {
                    JSONArray jsonArray = new JSONArray(String.valueOf(util.getHttpJsonByhttpclient(application.getWebURL() + uri)));
                    String status = null;
                    String msg = null;
                    List<Map<String, String>> list = null;
                    if (jsonArray.length() > 1) {
                        list = new ArrayList<>();
                    }
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject2 = (JSONObject) jsonArray.opt(i);
                        if (i == 0) {
                            status = jsonObject2.getString("status");
                            msg = jsonObject2.getString("msg");
                        } else {
                            if (kind[index] == KIND_KIND) {
                                Map<String, String> map = new HashMap<>();
                                map.put("kind_id", jsonObject2.getString("kind_id"));
                                map.put("kind_name", jsonObject2.getString("kind_name"));
                                list.add(map);
                            } else {
                                Map<String, String> map = new HashMap<>();
                                map.put("good_name", jsonObject2.getString("good_name"));
                                map.put("good_price", jsonObject2.getString("good_price"));
                                if (kind[index] == KIND_SALES) {
                                    map.put("good_pre", jsonObject2.getString("good_pre"));
                                }
                                if (kind[index] == KIND_TODAY) {
                                    map.put("good_mid", jsonObject2.getString("good_mid"));
                                } else {
                                    map.put("good_over", jsonObject2.getString("good_over"));
                                }
                                map.put("good_id", jsonObject2.getString("good_id"));
                                list.add(map);
                            }

                        }
                    }
                    if (status.equals("ok") && list != null) {
                        res++;
                        Map<String, Object> mapTemp = null;
                        switch (kind[index]) {
                            case KIND_TODAY:
                                for (Map<String, String> map : list) {
                                    mapTemp = new HashMap<>();
//                                    mapTemp.put("img",R.drawable.wait);
                                    mapTemp.put("imgBit", GetImage.getInternetPicture(getActivity(), imgUrlHead + "/" + map.get("good_id") + "_01.jpg"));
                                    mapTemp.put("name", map.get("good_name"));
                                    mapTemp.put("price", map.get("good_price"));
                                    mapTemp.put("id", map.get("good_id"));
                                    mapTemp.put("mid", map.get("good_mid"));
                                    todayList.add(mapTemp);
                                }
                                break;
                            case KIND_SALES:
                                for (Map<String, String> map : list) {
                                    mapTemp = new HashMap<>();
                                    mapTemp.put("imgBit", GetImage.getInternetPicture(getActivity(), imgUrlHead + "/" + map.get("good_id") + "_01.jpg"));
                                    mapTemp.put("name", map.get("good_name"));
                                    mapTemp.put("price", map.get("good_price"));
                                    mapTemp.put("over", map.get("good_over"));
                                    mapTemp.put("pre", map.get("good_pre"));
                                    mapTemp.put("id", map.get("good_id"));
                                    saleList.add(mapTemp);
                                }
                                break;
                            case KIND_NEW:
                                for (Map<String, String> map : list) {
                                    mapTemp = new HashMap<>();
                                    mapTemp.put("imgBit", GetImage.getInternetPicture(getActivity(), imgUrlHead + "/" + map.get("good_id") + "_01.jpg"));
                                    mapTemp.put("name", map.get("good_name"));
                                    mapTemp.put("price", map.get("good_price"));
                                    mapTemp.put("over", map.get("good_over"));
                                    mapTemp.put("id", map.get("good_id"));
                                    newList.add(mapTemp);
                                }
                                break;
                            case KIND_KIND:
                                Map<String, String> mapTemp2 = new HashMap<>();
                                for (Map<String, String> map : list) {
                                    mapTemp2.put(map.get("kind_name"), map.get("kind_id"));
                                }
                                application.setKindMap(mapTemp2);
                                break;
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            return res;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pd = new ProgressDialog(getContext());
            pd.setMessage("加载中...");
            if (!pd.isShowing()) {
                pd.show();
            }

        }

        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            int res = (int) o;
            if (res != 0) {
                myAdapterForToday.setList(todayList);
                myAdapterForToday.notifyDataSetChanged();
                myAdapterForSale.setList(saleList);
                myAdapterForSale.notifyDataSetChanged();
                myAdapterForNew.setList(newList);
                myAdapterForNew.notifyDataSetChanged();
                if (res != 3) {
                    Toast.makeText(getContext(), "加载失败！（" + (3 - res) + "个）", Toast.LENGTH_LONG);
                } else {
                    Toast.makeText(getContext(), "加载成功", Toast.LENGTH_LONG);
                }
            } else {
                Toast.makeText(getContext(), "加载失败！", Toast.LENGTH_LONG);
            }
            pd.dismiss();
            canRefresh = true;
        }
    }

}
